<?php

class LoginController extends Zend_Controller_Action 
{
	public function indexAction()
	{
		
	}
	
	public function loginAction()
	{
		$this->view->pageTitle = "Identification";
			
		$form = new Application_Form_Login();
		$this->view->form = $form;
		
		if ($this->_request->isPost ()) {
			$formData = $this->_request->getPost ();
			if ($form->isValid ( $formData )) {
				$login = $form->getValue('login');
				$salt = substr($login,0,3);
				$password = sha1($salt.$form->getValue('pass').$salt);
				
				$authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
				$authAdapter->setTableName('utilisateur')
					->setIdentityColumn('login')
					->setCredentialColumn('mdp')
					//->setCredentialTreatment('SHA1(?)')
					->setIdentity($login)
					->setCredential($password);
				
				$authAuthenticate = $authAdapter->authenticate();
				if ($authAuthenticate->isValid()) {
					$storage = Zend_Auth::getInstance()->getStorage();
					$storage->write($authAdapter->getResultRowObject(null,'mdp'));
					$this->_helper->redirector ( 'index', 'index' );
				} else {
					echo "Il n'existe pas d'utilisateur avec cet identifiant et/ou mot de passe";
				}
			}
		}
	}
	
	public function logoutAction() 
	{
		Zend_Auth::getInstance ()->clearIdentity ();
		$this->_helper->redirector ( 'index', 'index' );
	}
}

?>